package cn.yunyichina.provider.pay.listener;

import cn.yunyichina.provider.pay.service.FinanceService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.listener.MessageListener;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Joe.zhou on 2017/5/10.
 */
public class CheckAccountsListener implements MessageListener<Integer, String> {

    private static final Logger logger = LoggerFactory.getLogger(CheckAccountsListener.class);

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Resource
    private FinanceService financeService;

    @Override
    public void onMessage(ConsumerRecord<Integer, String> integerStringConsumerRecord) {
        String xmlString = integerStringConsumerRecord.value();
        logger.info("CheckAccountsListener.onMessage.xmlString -> {}", xmlString);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        JSONObject param = new JSONObject();
        param.put("begin_pay_date", format.format(c.getTime()));
        param.put("end_pay_date", format.format(c.getTime()));
        param.put("org_id", "ALL");
        try {
            financeService.createdFinanceSettingBill(JsonUtils.toJsonString(param));
        } catch (Exception e) {
            logger.error("生成对账错误", e);
        }
    }

}
